package com.dearbinge.opentapi.content.commodityspec;

import com.dearbinge.data.api.commodityspec.CommoditySpecService;
import com.dearbinge.data.pojo.commodityspec.CommoditySpec;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格控制层
 *
 * @author cl
 * @date 2019/3/07
 */
@Api(description = "商品规格")
@RestController
@RequestMapping("/commoditySpec")
@CrossOrigin
@EnableAutoConfiguration
public class CommoditySpecController {

    @Autowired
    private CommoditySpecService commoditySpecService;


    @ApiOperation(value = "增加修改商品规格", notes = "增加修改商品规格")
    @RequestMapping(value = "/insertOrUpdateCommoditySpec", method = RequestMethod.POST)
    public ResultMsg<String> insertOrUpdateCommoditySpec(@RequestBody CommoditySpec commoditySpec) {
        try {
            Integer dataCode;
            if (commoditySpec.getId() == null || commoditySpec.getId() == 0) {
                dataCode = commoditySpecService.insert(commoditySpec);
                if (dataCode > 0) {
                    return ResultMsg.ok("增加成功");
                }
            } else {
                dataCode = commoditySpecService.update(commoditySpec);
                if (dataCode > 0) {
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



    /**
     * 删除
     */
    @ApiOperation(value = "删除商品规格", notes = "根据ID删除商品规格")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = commoditySpecService.delete(id);
            if (dataCode > 0) {
                return ResultMsg.ok("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500, "系统异常");
    }

    /**
     * 根据id查询商品规格
     */
    @ApiOperation(value = "根据商品id查询商品规格", notes = "根据商品id查询商品规格")
    @ApiImplicitParam(name = "id", value = "商品id", paramType = "query")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public ResultMsg<CommoditySpec> selectById(Integer id) {
        try {
            CommoditySpec commoditySpec = commoditySpecService.selectById(id);
            return ResultMsg.ok(commoditySpec);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500, "系统异常");
    }


    /**
     * 根据商品id查询商品规格
     */
    @ApiOperation(value = "根据商品id查询商品规格", notes = "根据商品id查询商品规格")
    @ApiImplicitParam(name = "commodityId", value = "商品id", paramType = "query")
    @RequestMapping(value = "/selectByCommodityId", method = RequestMethod.GET)
    public ResultMsg<List<CommoditySpec>> selectByCommodityId(Integer commodityId) {
        try {
            List<CommoditySpec> commoditySpec = commoditySpecService.selectByCommodityId(commodityId);
            return ResultMsg.ok(commoditySpec);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(500, "系统异常");
    }




}
